<template>
  <div>
    <el-row>
      <el-col>
        <my-form
          :formOptions="findFormOptions"
          :isInline="true"
          :isSearch="true"
          @sureSubmit="findTableData"
        >
          <template #bottom_button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              type="primary"
              @click="reset"
              >重置</el-button
            >
          </template>
        </my-form>
      </el-col>
    </el-row>
    <el-row type="flex" justify="end">
      <el-col :span="2">
        <el-button size="mini" @click="add" icon="el-icon-circle-plus" type="primary"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <my-table
          :hasBorder="true"
          :hasIndex="true"
          :hasPagination="true"
          :hasStripe="true"
          :hasSelection="false"
          :tableColumnOptions="tableColumnOptions"
          :tableData="tableData"
        >
        <template #proCategory="{data}">
          {{ leibie[data.proCategory] }}
        </template>
        <template #proUtils="{data}">
          {{ danwei[data.proUtils] }}
        </template>
          <template #operation="{ data }">
            <el-button type="text" @click="update(data)">编辑</el-button>
            <el-button type="text" @click="remove(data)">删除</el-button>
          </template>
        </my-table>
      </el-col>
    </el-row>
    <my-dialog :dialogVisible.sync="infoDialog" :title="infoTitle">
      <template #content>
        <my-form
          :form="isAddInfo ? copyForm : form"
          :formOptions="infoFormOptions"

          :rules="infoRules"
          @sureSubmit="infoFormSubmit"
          ref="infoForm"
        >
        <template #proLeader="{ data }">
            <el-autocomplete
              v-model="test1"
              :fetch-suggestions="querySearch"
              placeholder="请输入内容"
              @select="handleSelect_person"
            ></el-autocomplete>
          </template>
        </my-form>
      </template>
    </my-dialog>
  </div>
</template>

<script>
import request from '@/utils/request' 
import myForm from "@/components/myForm.vue";
import myTable from "@/components/myTable.vue";
import myDialog from "@/components/myDialog.vue";
import { projectCate,getModelByLabel,getLabelByModel} from "@/utils/metaInfo"
import {
  A_tableData,
  A_add,
  A_update,
  A_remove,
  A_getUtil,
  A_changeUtil,
} from "@/api/infoMange/professionalInformation";
import { A_getUserList } from "@/utils/aboutUsers";
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "professionalInformation",
  inject: ["reload"],
  data() {
    return {
      findFormOptions: [{ type: "input", label: "专业名称", model: "proName" }],
      tableColumnOptions: [
        {
          label: "专业名称",
          prop: "proName",
          width: "150",
        },
        {
          label: "专业代码",
          prop: "proId",
          width: "150",
        },
                {
          // isSlot:true,
          slotName:'proUtils',
          label: "所属单位名称",
          prop: "proUtils",
          width: "150",
        },
        {
          label: "负责人姓名",
          prop: "name",
          width: "150",
        },
        {
          label: "负责人",
          prop: "proLeader",
          width: "150",
        },
        {
          label: "类别",
          isSlot:true,
          slotName:'proCategory',
          prop: "proCategory",
          width: "150",
        },

        {
          label: "操作",
          width: "180",
          isSlot: true,
          slotName: "operation",
          fixed: "right",
        },
      ],
      tableData: [],
      infoDialog: false,
      infoTitle: "编辑操作",
      isAddInfo: false,
      form: {},
      copyForm: {},
      nowDropItem: "",
      infoFormOptions: [
      {
          type: "radio",
          label: "类别",
          model: "proCategory",
          width:500,
          options: projectCate
        },
        {
          type: "input",
          label: "专业名称",
          model: "proName",
          
        },
        {
          type: "input",
          label: "专业代码",
          model: "proId",
          
        },
        {
          type: "slot",
          label: "负 责 人 ",
          slotName:'proLeader',
          model: "proLeader",
        },
        {
          type: "select",
          label: "所属单位名称",
          model: "proUtils",
          options:[]
        },
      ],
      infoRules: {
        proName: [{ required: true, message: "请输入专业名称" }],
        proId: [{ required: true, message: "请输入专业代码" }],
        proLeader: [{ required: false, message: "请输入负责人" }],
        proCategory: [{ required: true, message: "请选择类别" }],
        proUtils: [{ required: true, message: "请输入所属单位" }],
      },
      test1: "",
      user_list: null,
      leibie:["自然科学",'人文社科','管理部门'],
      danwei:['院系','职能部门']
    };
  },
  components: {
    myForm,
    myTable,
    myDialog,
  },
 created() {
    this.getTableData();
    this.getOptions();
    console.log("专业信息")
  },
  methods: {
    async getOptions(){
      let options = await request({
         method: 'post',
          url: '/SciUtils/list',
          data:{}
      })
      // console.log("options",options)
      let arr = []
      options.data.forEach(item => {
        arr.push({ label: item.utilsName, model: item.utilsKey })
      })
      this.infoFormOptions[4].options = arr
    },
    async getUserList() {
      let result = await A_getUserList();
      result.forEach((item) => {
        const value=item.userId+' : '+item.userName
        item.value=value
      });
      this.user_list=result
    },
    async findTableData(data) {
      let result = await A_tableData(data);
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async getTableData() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);

      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async reset() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    add() {
      this.isAddInfo = true;
      this.infoDialog = true;
      this.getUserList();
    },
    update(data) {
      data.proCategory = getLabelByModel(data.proCategory,projectCate)
      console.log(data)
      this.isAddInfo = false;
      this.infoDialog = true;
      this.form = cloneDeep(data);
      this.getUserList();
    },
    remove(data) {
      this.$confirm("你确定要删除该信息吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { proKey } = data;
          let res = await A_remove(proKey);
          if (res.code === 1) {
            this.$message({ type: "success", message: res.data });
            this.reload();
          } else {
            this.$message({ type: "error", message: res.msg });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async infoFormSubmit(data) {
      data.proCategory = getModelByLabel(data.proCategory,projectCate)
      if (this.isAddInfo) {
        let res = await A_add(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      } else {
        let res = await A_update(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      }
      // this.$refs.filesUpload.submit();
    },
    async getUtil() {
      let result = await A_getUtil();
      this.nowDropItem = result;
    },
    querySearch(queryString, cb) {
      var user_list = this.user_list;
      var results = queryString
        ? user_list.filter(this.createFilter(queryString))
        : user_list;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (user_list) => {
        return (
          user_list.userName.indexOf(queryString) === 0
        );
      };
    },
    handleSelect_person(item) {
        
      this.$refs.infoForm.form.proLeader = item.userId;
    },
  },
  computed: {},
};
</script>

<style scoped>
.upload-demo {
  display: inline-block;
  margin: 0 3px;
}
</style>